# -*- coding: UTF-8 -*-   

from py2neo import Graph

# 这个语句我进行了下改造：就是他返回的的东西全部变成字符串

class AnswerSearcher:
    def __init__(self):
        # self.g = Graph("http://localhost:7474", auth=("ruanxie", "123456")) # 这里我切换了下数据库
        self.g = Graph("http://localhost:7474", auth=("neo4j","123456"))
        self.num_limit = 20

    '''执行cypher查询，并返回相应结果'''
    def search_main(self, sqls):
        final_answers = []
        for sql_ in sqls:
            question_type = sql_['question_type']
            queries = sql_['sql']
            answers = []
            for query in queries:
                ress = self.g.run(query).data()
                answers += ress
            final_answer = self.answer_prettify(question_type, answers)
        # 原本有下面那几句，现在不需要了
            # if final_answer:
            #     final_answers.append(final_answer)
        # return final_answers
        return final_answer

    '''根据对应的qustion_type，调用相应的回复模板'''
    def answer_prettify(self, question_type, answers):
        final_answer = []
        if not answers:
            return ''

        
        #知识单元
        if question_type == 'zhishidanyuan':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}课程包含的知识单元如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的知识点有哪些？2.{0}的简介3.{0}的教学大纲'.format(i)

                print(final_answer)
                print(want_find)
      
        # #介绍
        elif question_type == 'jieshao':
            l_ = []
            for i in answers:
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
                    final_answer = '{0}的简介如下：{1}'.format(i['m.name'], i['m.jieshao'])
                    # print(i,i['m.name'])
                    want_find='您可能想查找：1.{0}的知识单元有哪些？2.{0}的学时是多少？3.{0}的先修课程有什么？'.format(i['m.name'])
                    print(final_answer)
                    print(want_find)
        #学时
        elif question_type == 'xueshi':
            l_ = []
            for i in answers:
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
                    final_answer = '{0}的学时是：{1}'.format(i['m.name'], i['m.xueshi'])
                    want_find='您可能想查找：1.{0}的简介？2.{0}的后继课程有什么？3.{0}的先修课程有什么？'.format(i['m.name'])

                    print(final_answer)
                    print(want_find)

        #教学大纲
        elif question_type == 'dagang':
            l_ = []
            for i in answers:
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
                    final_answer = '{0}的教学大纲是：{1}'.format(i['m.name'], i['m.jiaoxuedagang'])
                    want_find='您可能想查找：1.{0}的简介？2.{0}的知识单元有哪些？3.{0}的知识点有哪些？'.format(i['m.name'])

                    print(final_answer)
                    print(want_find)

        #先修课程
        elif question_type == 'xianxiukecheng':
            l_ = []
            for i in answers:
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
                    final_answer = '{0}的先修课程如下：{1}'.format(i['m.name'], i['b.name'])
                    want_find='您可能想查找：1.{0}的后继课程有哪些？2.{0}的简介？3.{0}的教学大纲？'.format(i['m.name'])

                    print(final_answer)
                    print(want_find)


        #知识点
        elif question_type == 'zhishidian':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}包含的知识点如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的知识单元有哪些？2.{0}的简介？3.{0}的教学大纲？'.format(i)

                print(final_answer)
                print(want_find)


        #归属
        elif question_type == 'guishu':
            l_ = []
            for i in answers:
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
                    final_answer = '{0}属于：{1}'.format(i['m.name'], i['b.name'])
                    want_find = '您可能想查找：1.{0}的学分是多少？2.{0}的简介？3.{0}的教学大纲？'.format(i['m.name'])

                    print(final_answer)
                    print(want_find)

        #掌握程度
        elif question_type == 'zhangwochengdu':
            l_ = []
            for i in answers:
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
                    final_answer = '{0}要求的掌握程度如下：{1}'.format(i['m.name'], i['m.掌握程度'])
                    want_find = '您可能想查找：1.{0}的重要程度2.{0}的后继知识点？3.{0}的前置知识点？'.format(i['m.name'])

                    print(final_answer)
                    print(want_find)

        #重要程度
        elif question_type == 'zhongyaochengdu':
            l_ = []
            for i in answers:
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
                    final_answer = '{0}的重要程度如下：{1}'.format(i['m.name'], i['m.重要程度'])
                    want_find = '您可能想查找：1.{0}的掌握程度2.{0}的后继知识点？3.{0}的前置知识点？'.format(i['m.name'])

                    print(final_answer)
                    print(want_find)
        #前置课程
        elif question_type == 'qianzhikecheng':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}的前置课程如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的后继课程是什么2.{0}包含哪些知识点？3.{0}包含哪些知识单元？'.format(i)

                print(final_answer)
                print(want_find)
        #后继课程
        elif question_type == 'houjikecheng':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}的后继课程如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的前置课程是什么2.{0}包含哪些知识点？3.{0}包含哪些知识单元？'.format(i)

                print(final_answer)
                print(want_find)
        # #后继课程
        # elif question_type == 'houjikecheng':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的后继课程如下：{1}'.format(i['m.name'], i['b.name'])
        #             want_find = '您可能想查找：1.{0}的前置课程是什么2.{0}包含哪些知识点？3.{0}包含哪些知识单元？'.format(i['m.name'])
        #
        #             print(final_answer)
        #             print(want_find)
        #
        # #前置课程
        # elif question_type == 'qianzhikecheng':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的前置课程如下：{1}'.format(i['m.name'], i['b.name'])
        #             want_find = '您可能想查找：1.{0}的后继课程是什么2.{0}包含哪些知识点？3.{0}包含哪些知识单元？'.format(i['m.name'])
        #
        #             print(final_answer)
        #             print(want_find)
        #前置知识点
        elif question_type == 'qianzhizhishidian':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}的前置知识点如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的后继知识点是什么2.{0}要求的掌握程度？3.{0}的重要程度？'.format(i)

                print(final_answer)
                print(want_find)
        #后继知识点
        elif question_type == 'houjizhishidian':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}的后继知识点如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的前置知识点是什么2.{0}要求的掌握程度？3.{0}的重要程度？'.format(i)

                print(final_answer)
                print(want_find)
        # #后继知识点
        # elif question_type == 'houjizhishidian':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的后继知识点如下：{1}'.format(i['m.name'], i['b.name'])
        #             want_find = '您可能想查找：1.{0}的前置知识点是什么2.{0}要求的掌握程度？3.{0}的重要程度？'.format(i['m.name'])
        #
        #             print(final_answer)
        #             print(want_find)
        #
        # #前置知识点
        # elif question_type == 'qianzhizhishidian':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的前置知识点如下：{1}'.format(i['m.name'], i['b.name'])
        #             want_find = '您可能想查找：1.{0}的后继知识点是什么2.{0}要求的掌握程度？3.{0}的重要程度？'.format(i['m.name'])
        #
        #             print(final_answer)
        #             print(want_find)
        #前置知识单元
        elif question_type == 'qianzhizhishidanyuan':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}的前置知识单元如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}涵盖哪些知识点？3.{0}属于什么课程？'.format(i)

                print(final_answer)
                print(want_find)
        #后继知识单元
        elif question_type == 'houjizhishidanyuan':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}的后继知识单元如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}涵盖哪些知识点？3.{0}属于什么课程？'.format(i)

                print(final_answer)
                print(want_find)
        # #后继知识单元
        # elif question_type == 'houjizhishidanyuan':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的后继知识单元如下：{1}'.format(i['m.name'], i['b.name'])
        #             want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}涵盖哪些知识点？3.{0}属于什么课程？'.format(i['m.name'])
        #
        #             print(final_answer)
        #             print(want_find)
        #
        #
        # #前置知识单元
        # elif question_type == 'qianzhizhishidanyuan':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的前置知识单元如下：{1}'.format(i['m.name'], i['b.name'])
        #             want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}涵盖哪些知识点？3.{0}属于什么课程？'.format(i['m.name'])
        #
        #             print(final_answer)
        #             print(want_find)
        #知识单元所含知识点
        elif question_type == 'ZSDYZSD':
            dict_ = {}
            for i in answers:
                if i['m.name'] not in dict_:
                    dict_[i['m.name']] = i['b.name']
                else:
                    dict_[i['m.name']] += ("、" + i['b.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}所含的知识点如下：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}的后继知识单元是什么？3.{0}属于什么课程？'.format(i)

                print(final_answer)
                print(want_find)
        # #知识单元所含知识点
        # elif question_type == 'ZSDYZSD':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}所含的知识点如下：{1}'.format(i['m.name'], i['b.name'])
        #             want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}的后继知识单元是什么？3.{0}属于什么课程？'.format(i['m.name'])
        #
        #             print(final_answer)
        #             print(want_find)
        #知识单元所属课程
        elif question_type == 'ZSDYKC':
            dict_ = {}
            for i in answers:
                if i['b.name'] not in dict_:
                    dict_[i['b.name']] = i['m.name']
                else:
                    dict_[i['b.name']] += ("、" + i['m.name'])
            #print(dict_)
            for i in dict_:
                final_answer = '{0}属于如下课程：{1}'.format(i, dict_[i])
                want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}的后继知识单元是什么？3.{0}涵盖哪些知识点？'.format(i)

                print(final_answer)
                print(want_find)
        # #
        # elif question_type == 'ZSDYKC':
        #     l_ = []
        #     for i in answers:
        #
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}属于如下课程：{1}'.format(i['b.name'], i['m.name'])
        #             want_find = '您可能想查找：1.{0}的前置知识单元是什么2.{0}的后继知识单元是什么？3.{0}涵盖哪些知识点？'.format(i['m.name'])
        #             print(i)
        #             print(final_answer)
        #             print(want_find)
        #课程所含知识单元数量
        elif question_type == 'ZSDYSL':
            l_ = []
            num = 0
            for i in answers:
                num+=1
                # print(num)
                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
            final_answer = '{0}有{1}个知识单元'.format(i['m.name'],num)
            want_find = '您可能想查找：1.{0}有多少个知识点2.{0}的后继课程是什么？3.{0}的前置课程是什么？'.format(i['m.name'])
            print(final_answer)
            print(want_find)
        #课程所含知识点数量
        elif question_type == 'ZSDSL':
            l_ = []
            num = 0
            for i in answers:
                num += 1

                if i['m.name'] not in l_:
                    l_.append(i['m.name'])
            final_answer = '{0}有{1}个知识点'.format(i['m.name'], num)
            want_find = '您可能想查找：1.{0}有多少知识单元2.{0}的后继课程是什么？3.{0}的前置课程是什么？'.format(i['m.name'])

            print(final_answer)
            print(want_find)
        # print()
        # #职称
        # elif question_type == 'zhicheng':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的职称是{1}'.format(i['m.name'], i['m.zhicheng'])
        #             print(final_answer)
        # #教师年龄
        # elif question_type == 'jiaoshinianling':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的年龄是{1}岁'.format(i['m.name'], i['m.nianling'])
        #             print(final_answer)
        # #教师性别
        # elif question_type == 'jiaoshixingbie':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的性别为{1}'.format(i['m.name'], i['m.xingbie'])
        #             print(final_answer)
        # #学生学号
        # elif question_type == 'xuehao':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的学号为{1}'.format(i['m.name'], i['m.xuehao'])
        #             print(final_answer)
        # #学生性别
        # elif question_type == 'xueshengxingbie':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的性别为{1}'.format(i['m.name'], i['m.xingbie'])
        #             print(final_answer)
        # #学生年龄
        # elif question_type == 'xueshengnianling':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}的年龄是{1}岁'.format(i['m.name'], i['m.nianling'])
        #             print(final_answer)
        # #学生年级
        # elif question_type == 'xueshengnianji':
        #     l_ = []
        #     for i in answers:
        #         if i['m.name'] not in l_:
        #             l_.append(i['m.name'])
        #             final_answer = '{0}所在的年级为：{1}'.format(i['m.name'], i['m.nianji'])
        #             print(final_answer)
        # #学生选修课程
        # elif question_type == 'xueshengxuanxiukecheng':
        #     dict_ = {}
        #     for i in answers:
        #         if i['m.name'] not in dict_:
        #             dict_[i['m.name']] = i['b.title']
        #         else:
        #             dict_[i['m.name']] += ("、" + i['b.title'])
        #     #print(dict_)
        #     for i in dict_:
        #         final_answer = '{0}的所选课程如下：{1}'.format(i, dict_[i])
        #         print(final_answer)
        # #教师教授课程
        # elif question_type == 'jiaoshijiaoshoukecheng':
        #     dict_ = {}
        #     for i in answers:
        #         if i['m.name'] not in dict_:
        #             dict_[i['m.name']] = i['b.title']
        #         else:
        #             dict_[i['m.name']] += ("、" + i['b.title'])
        #     #print(dict_)
        #     for i in dict_:
        #         final_answer = '{0}讲授的课程如下：{1}'.format(i, dict_[i])
        #         print(final_answer)
        # #学生所属专业
        # elif question_type == 'xueshengsuoshuzhuanye':
        #     dict_ = {}
        #     for i in answers:
        #         if i['m.name'] not in dict_:
        #             dict_[i['m.name']] = i['b.name']
        #         else:
        #             dict_[i['m.name']] += ("、" + i['b.name'])
        #     #print(dict_)
        #     for i in dict_:
        #         final_answer = '{0}同学所属专业为：{1}'.format(i, dict_[i])
        #         print(final_answer)
        # #教师管理的学生
        # elif question_type == 'jiaoshiguanlixuesheng':
        #     dict_ = {}
        #     for i in answers:
        #         if i['m.name'] not in dict_:
        #             dict_[i['m.name']] = i['b.name']
        #         else:
        #             dict_[i['m.name']] += ("、" + i['b.name'])
        #     #print(dict_)
        #     for i in dict_:
        #         final_answer = '{0}老师管理的学生是：{1}'.format(i, dict_[i])
        #         print(final_answer)
        # #后继课程
        # elif question_type == 'houjikecheng':
        #     dict_ = {}
        #     for i in answers:
        #         if i['m.title'] not in dict_:
        #             dict_[i['m.title']] = i['b.title']
        #         else:
        #             dict_[i['m.title']] += ("、" + i['b.title'])
        #     #print(dict_)
        #     for i in dict_:
        #         final_answer = '{0}的后继课程有：{1}'.format(i, dict_[i])
        #         print(final_answer)

        # return final_answer# 一开始返回的
     
        return f"答案如下：\n{final_answer}您可能想查找：\n{want_find}"


if __name__ == '__main__':
    searcher = AnswerSearcher()

